$(function () {
    $("#jqGrid").jqGrid({
        url: '../filemanagement/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '文件名', name: 'fileName', index: 'file_name', width: 80 },
            { label: 'URL地址', name: 'url',width: 160 },
			{ label: '上传时间', name: 'uploadTime', index: 'uploadTime', width: 80 },
            { label: '操作', width: 40,formatter: download}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        showList: true,
        title: null,
        fileManagement: {},
        fileList: [{//数据结构
            name: '',
            url: ''
        },
            {
                name: '',
                url: ''
            }
        ]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fileManagement = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.fileManagement.id == null ? "../filemanagement/save" : "../filemanagement/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.fileManagement),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../filemanagement/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../filemanagement/info/"+id, function(r){
                vm.fileManagement = r.fileManagement;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},

        handleRemove:function(file, fileList) {
            //删除文件
            //console.log(file, fileList);
        },
        handlePreview:function(file) {
            alert("q");

        }
	}
});


$("#uploadwin").click(function(){
    // utils.openDialogView('上传附件','uploadwin.html',"500px","300px");
    vm.showList = false;
    vm.title = "上传";
    var page = $("#jqGrid").jqGrid('getGridParam','page');
    $("#jqGrid").jqGrid('setGridParam',{
        page:page
    }).trigger("reloadGrid");
});
function download(cellvalue, options, rowObject){
    var html = "";
    html = "<a class='btn btn-primary' onclick='Click(\""+rowObject.id+"\")'>下载</a>";
    return html;

}
function Click(id){
    url="../filemanagement/download?id="+id;
    window.location.href=url;
}


function preview(file) {
    var prevDiv = document.getElementById('preview');
    if (file.files && file.files[0]) {
        var reader = new FileReader();
        reader.onload = function(evt) {
            prevDiv.innerHTML = '<img src="' + evt.target.result + '" />';
        }
        reader.readAsDataURL(file.files[0]);
    } else {
        prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
    }
}


function doUpload() {
    vm.showList = false;
    var formData = new FormData($( "#uploadForm" )[0]);
    $.ajax({
        url: '../filemanagement/upload' ,
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(r){
            if(r.code === 0){
                alert('上传成功', function(){
                    //关闭当前窗口页面
                   // utils.closeCurrentPage();
                    //刷新父页面
                    //  parent.location.reload();
                    vm.reload();
                });
            }else{
                alert(r.msg);
            }
        }
    });
}
